import random
from PIL import Image
from typing import List, Dict
import requests

# 模板列表
preprompts = [
    "<img>",
    "Here is an image: <img>",
    "Look at this picture: <img>",
    "This is a photo: <img>",
    "Observe this image: <img>",
    "Take a look at this snapshot: <img>",
    "Here is a visual for you: <img>",
    "Check out this image: <img>",
    "Have a look at this picture: <img>",
    "Notice this photograph: <img>",
    "Please observe this photo: <img>",
    "See this image: <img>",
    "Here's a picture for you to view: <img>",
    "This image might interest you: <img>",
    "I've got a photo here: <img>"
]

postprompts = [
    "<\\img> Describe what you see.",
    "<\\img> Can you describe this picture?",
    "<\\img> What is in this image?",
    "<\\img> Tell me about this photo.",
    "<\\img> What do you observe in this picture?",
    "<\\img> What do you notice in this image?",
    "<\\img> How would you describe this scene?",
    "<\\img> What details can you spot?",
    "<\\img> Can you explain what’s happening here?",
    "<\\img> What catches your eye in this picture?",
    "<\\img> What stands out to you in this image?",
    "<\\img> Can you identify what’s in the photo?",
    "<\\img> What elements do you see in this picture?",
    "<\\img> How would you interpret this photo?",
    "<\\img> What is your observation of this image?"
]

sysprompts = [
    "You are a visual assistant who understands pictures between <img><\\img> tags.",
    "As a visual assistant, process and interpret images within <img><\\img> tags with text.",
    "You are a visual assistant. Analyze images in <img><\\img> tags and integrate with text.",
    "As a visual assistant, integrate image data from <img><\\img> tags with text context.",
    "You understand images between <img><\\img> tags. Combine visual content with text.",
    "Interpret images in <img><\\img> tags and enhance your responses with this visual data."
]


# 准备训练数据的函数
def prepare_training_data(image: Image.Image, descriptions: List[str]) -> List[Dict]:
    training_data = []
    num_samples = len(descriptions)
    for i in range(num_samples):
        pre_prompt = random.choice(preprompts)
        post_prompt = random.choice(postprompts)
        sys_prompt = random.choice(sysprompts)
        description = descriptions[i]

        messages = [
            {"role": "system", "content": sys_prompt},
            {"role": "user", "content": f"{pre_prompt}{post_prompt}"},
            {"role": "assistant", "content": description}
        ]

        training_data.append({
            "image": image,
            "messages": messages
        })

    return training_data

def prepare_chat_messages(inputs_prompt: str) -> List[Dict]:

    pre_prompt = random.choice(preprompts)
    sys_prompt = random.choice(sysprompts)
    
    messages = [
        {"role": "system", "content": sys_prompt},
        {"role": "user", "content": f"{pre_prompt}{inputs_prompt}"},
    ]

    return messages
